package com.chinaisafe.user.controller;

import com.chinaisafe.base.common.validated.AddGroup;
import com.chinaisafe.base.common.validated.QueryGroup;
import com.chinaisafe.base.common.validated.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.chinaisafe.base.common.annotation.OperationLog;
import com.chinaisafe.base.common.enums.BusinessTypeEnum;
import com.chinaisafe.base.common.vo.Answer;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.chinaisafe.user.vo.request.SysAppReqVO;
import com.chinaisafe.user.vo.response.SysAppRespVO;
import org.springframework.validation.annotation.Validated;
import com.chinaisafe.user.service.SysAppService;
import org.springframework.web.bind.annotation.RestController;

/**
 * SysAppController
 * Description：应用接口层
 *
 * @author tby
 * @date  2024-07-11 17:32:41
 */
@RestController
@RequestMapping("/sysApp")
@Tag(name = "平台级应用管理", description = "平台级应用管理")
public class SysAppController {

    @Autowired
    private SysAppService sysAppService;

    @GetMapping("/pageList")
    @Operation(summary = "获取应用分页列表", description = "获取应用分页列表")
    public Answer<IPage<SysAppRespVO>> pageList(@Validated(QueryGroup.class) SysAppReqVO sysAppReqVO) {
        IPage<SysAppRespVO> pageList = sysAppService.getPageList(sysAppReqVO);
        return Answer.success("获取成功", pageList);
    }

    @PostMapping()
    @Operation(summary = "新增应用", description = "新增应用")
    @OperationLog(title = "应用-新增", businessType = BusinessTypeEnum.INSERT)
    public Answer<SysAppRespVO> add(@RequestBody @Validated(AddGroup.class) SysAppReqVO sysAppReqVO) {
        SysAppRespVO sysAppRespVO = sysAppService.add(sysAppReqVO);
        return Answer.success("保存成功", sysAppRespVO);
    }

    @PutMapping()
    @Operation(summary = "根据id编辑应用", description = "根据id编辑应用")
    @OperationLog(title = "应用-根据id编辑", businessType = BusinessTypeEnum.UPDATE)
    public Answer<SysAppRespVO> update(@RequestBody @Validated(UpdateGroup.class) SysAppReqVO sysAppReqVO) {
        SysAppRespVO sysAppRespVO = sysAppService.update(sysAppReqVO);
        return Answer.success("修改成功", sysAppRespVO);
    }

    @GetMapping("/{sysAppId}")
    @Operation(summary = "根据id获取应用", description = "根据id获取应用")
    public Answer<SysAppRespVO> getDetailById(@PathVariable Long sysAppId) {
        SysAppRespVO sysAppRespVO = sysAppService.getDetailById(sysAppId);
        return Answer.success("获取成功", sysAppRespVO);
    }

    @PostMapping("enableSwitch")
    @Operation(summary = "平台应用启用/禁用", description = "参数请放在?后面")
    public Answer<Void> enableSwitch(Long sysAppId) {
        sysAppService.enableSwitch(sysAppId);
        return Answer.success("操作成功");
    }

    @DeleteMapping()
    @Operation(summary = "根据id删除应用", description = "根据id删除应用")
    @OperationLog(title = "应用-根据id删除", businessType = BusinessTypeEnum.DELETE)
    public Answer<Void> deleteByIds(@RequestParam("ids") Long[] ids) {
        sysAppService.deleteByIds(ids);
        return Answer.success("删除成功");
    }
}

